import java.util.*;


public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
     *
     * 
     * @param head ListNode类 
     * @param n int整型 
     * @return ListNode类
     */
    public ListNode removeNthFromEnd (ListNode head, int n) {
        // write code here
         ListNode slowPrev = null ;
          ListNode slow = head ;
          ListNode fast = head ;
          while(n>0){
            fast = fast.next ;
            n-- ;
          }
        while(fast != null){
          slowPrev = slow ;
          slow = slow.next ;
          fast = fast.next ;
        }
        if(slowPrev !=null){
            slowPrev.next = slow.next;
        }else if(slow == null){
            return head ;
        }else{
            head = head.next ;
        }

       return head ;

    }
}